import scrapy
import re
from spider.items import LearnkuItem


class LearnkuSpider(scrapy.Spider):
    name = 'learnku'
    allowed_domains = ['learnku.com']
    # learnku laravel社区的点赞数量最多的文章
    start_urls = ['https://learnku.com/laravel?order=vote']

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url=url, callback=self.parse_vote_url)

    def parse_vote_url(self, response):
        """
        分析点赞数量最高的链接，拿到文章地址
        :param response:
        :return:
        """
        article_list = response.xpath("//div[contains(@class, 'topic-list')]//div[contains(@class, 'simple-topic')]")
        for article_item in article_list:
            learku_item = LearnkuItem()
            learku_item['author'] = article_item.xpath(
                ".//div[contains(@class, 'user-avatar')]//img[contains(@class, 'topic-list-author-avatar')]/@alt"
            ).extract_first()
            learku_item['category_name'] = article_item.xpath(
                ".//div[contains(@class, 'category-name')]//a/text()"
            ).extract_first().strip()
            article_a = article_item.xpath(".//a[contains(@class, 'topic-title-wrap')]")
            learku_item['url'] = article_a[0].attrib['href']
            learku_item['article_id'] = re.findall('t/([0-9]+)/', learku_item['url'])[0]
            learku_item['title'] = article_a.xpath(
                ".//span[contains(@class, 'topic-title')]/text()"
            ).extract_first().strip()
            learku_item['vote_number'] = article_a.xpath(
                ".//span[contains(@class, 'simple-topic-meta')]//span[contains(@class, 'count_of_replies')]/text()"
            ).extract()[1].strip()
            yield scrapy.Request(url=learku_item['url'], callback=self.parse_article_url, cb_kwargs={'item': learku_item})
            pass

    def parse_article_url(self, response, item):
        article_body = response.xpath("//div[contains(@class, 'article-content')]")
        article_meta = article_body.xpath(".//*[contains(@class, 'book-article-meta')]")
        text_mutes = article_meta.xpath(".//span[contains(@class, 'text-mute')]/text()").extract()
        item['views'] = text_mutes[0].strip()
        item['reply_number'] = text_mutes[1].strip()
        item['released_at'] = article_meta.xpath(".//a[last()]//span/@title").extract_first()
        item['content'] = article_body.xpath(".//div[contains(@class, 'content-body')]").extract_first()
        yield item
